package com.xxxx.seckill.controller;

import com.xxxx.seckill.pojo.Goods;
import com.xxxx.seckill.pojo.User;
import com.xxxx.seckill.service.IGoodsService;
import com.xxxx.seckill.service.IUserService;
import com.xxxx.seckill.vo.DetailVo;
import com.xxxx.seckill.vo.GoodsVo;
import com.xxxx.seckill.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.concurrent.TimeUnit;

//跳转商品列表
//  windows 30000请求 优化前  1332
//                   页面缓存后 2342
@Controller
@RequestMapping("/goods")
public class GoodsController {
@Autowired
    IUserService iUserService;
@Autowired
    IGoodsService iGoodsService;
@Autowired
    RedisTemplate redisTemplate;
@Autowired
    ThymeleafViewResolver thymeleafViewResolver;

@RequestMapping(value = "/toList",produces = "text/html;charset=utf-8")
@ResponseBody
public String toList(Model model, User user, HttpServletRequest request, HttpServletResponse response)
{
    ValueOperations valueOperations = redisTemplate.opsForValue ( );
    String html = (String) valueOperations.get ("goodslist");
    if (!StringUtils.isEmpty (html)){
        return html;
    }
    model.addAttribute ("user",user);
    model.addAttribute("goodsList",iGoodsService.findGoodsVo());
    WebContext context = new WebContext(request, response, request.getServletContext(), request.getLocale(),
            model.asMap());
    html = thymeleafViewResolver.getTemplateEngine ( ).process ("goodsList", context);
    if (!StringUtils.isEmpty (html)){
        valueOperations.set ("goodsList",html,60, TimeUnit.SECONDS);
    }
    return html;
    }


    @RequestMapping(value = "/detail/{goodsId}")
    @ResponseBody
    public RespBean toDetail( User user, @PathVariable Long goodsId){
        GoodsVo goodsVo = iGoodsService.findGoodsVoByGoodsId (goodsId);
        Date startDate = goodsVo.getStartDate ( );
        Date endDate = goodsVo.getEndDate ( );
        Date nowdate = new Date ( );
//        秒杀状态
        int secKillStatus=0;
//        秒杀倒计时
        int remainSeconds=0;
        if (nowdate.before (startDate)){
            remainSeconds= ((int)((startDate.getTime () - nowdate.getTime ()) /1000));
        }else if (nowdate.after (endDate)){
            secKillStatus=2;
            remainSeconds=-1;
        }else {
            secKillStatus=1;
            remainSeconds=0;
        }
        DetailVo detailVo = new DetailVo ( );
        detailVo.setUser (user);
        detailVo.setGoodsVo (goodsVo);
        detailVo.setRemainSeconds (remainSeconds);
        detailVo.setSecKillStatus (secKillStatus);
        return RespBean.success (detailVo);
    }

    @RequestMapping(value = "/toDetail/{goodsId}",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toDetail2(Model model,User user,@PathVariable Long goodsId,HttpServletRequest request, HttpServletResponse response){
        ValueOperations valueOperations = redisTemplate.opsForValue ( );
        String html = (String) valueOperations.get ("goodsDetail"+goodsId);
        if (!StringUtils.isEmpty (html)){
            return html;
        }
        model.addAttribute ("user",user);
        GoodsVo goodsVo = iGoodsService.findGoodsVoByGoodsId (goodsId);
        Date startDate = goodsVo.getStartDate ( );
        Date endDate = goodsVo.getEndDate ( );
        Date nowdate = new Date ( );
//        秒杀状态
        int secKillStatus=0;
//        秒杀倒计时
        int remainSeconds=0;
        if (nowdate.before (startDate)){
            remainSeconds= ((int)((startDate.getTime () - nowdate.getTime ()) /1000));
        }else if (nowdate.after (endDate)){
            secKillStatus=2;
            remainSeconds=-1;
        }else {
            secKillStatus=1;
            remainSeconds=0;
        }

        model.addAttribute ("remainSeconds",remainSeconds);
        model.addAttribute ("secKillStatus",secKillStatus);
        model.addAttribute ("goods",goodsVo);
        WebContext context = new WebContext(request, response, request.getServletContext(), request.getLocale(),
                model.asMap());
        html = thymeleafViewResolver.getTemplateEngine ( ).process ("goodsDetail", context);
        if (!StringUtils.isEmpty (html)){
            valueOperations.set ("goodsDetail"+goodsId,html,60, TimeUnit.SECONDS);
        }
        return html;
    }
}
